//乙方组织列表
//jQuery Datatable 对象
var yifangTable;
var tmpTable;
var btnModel = '    \
	{{#each func}}\
    <button type="button" class="{{this.type}} btn-sm" onclick="{{this.fn}}">{{this.name}}</button>\
    {{/each}}';
var template = Handlebars.compile(btnModel);
$(document).ready(function(){
	if($("#yifangTable").size() > 0){
		tmpTable = $("#yifangTable").DataTable({
			"ordering": false,// 排序
			"serverSide": false,// 开启服务器模式,    
	        "scrollX": true,// 横向滚动
			columns: [// 对应列
				{"data": null,"title":"操作",width:"*",className:"text-center"},
	            {"title":"组织名称",className: "text-center"}
	        ],
	        "data": [],
	        "dom": 'rt<"pull-left mt5"><"pull-right mt5"><"clear">' //生成样式
		});	
	}
	if($("#emptyStaffTable").size() > 0){
		$("#emptyStaffTable").parent("div").show();
		tmpTable = $("#emptyStaffTable").DataTable({
			"ordering": false,// 排序
	        "scrollX": true,// 横向滚动
	        "scrollCollapse": true,
			columns: [// 对应列
				{"data": null,"title":"操作",width:"*",className:"text-center"},
	            {"data": "","title":"人员姓名",className: "text-center"},
	            {"data": "","title":"人员账号",width:"120",className: "text-center"},
	            {"data": "","title":"岗位类别",className: "text-center"},
	            {"data": "","title":"所属组织",className: "text-center"}
	        ],
	        "columnDefs": [
				{// 所有列默认值
					"targets": "_all",
					"defaultContent": ''
				}]
			,"dom": 'rt<"bottom"><"clear">' //生成样式
		});	
	}
});

function EforgSearch(disId,yourFun){
	//window.open(prc+"/sysOrgController/toPartnerOrgList"+"?disId="+disId+"&yourFun="+yourFun);
	//window.showModalDialog(prc+"/sysOrgController/toPartnerOrgList"+"?disId="+disId+"&yourFun="+yourFun);
	var frameSrc =prc+"/sysOrgController/toPartnerOrgList"+"?disId="+disId+"&yourFun="+yourFun; 
    $("#NoPermissioniframe").attr("src", frameSrc);  
    $('#NoPermissionModal').modal({ show: true, backdrop: 'static' });
}
//查看乙方组织
function searchyifOrg(){
	 var disId = $("#disId").val();
	 var yourFun = $("#yourFun").val();
	if(null==disId||""==disId||undefined == disId){
		if($('#mytreeModal').size==0){
			 $('#mytreeModal').empty();
			 $('body').append("<div class='modal fade' id='mytreeModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>"+
			"<div class='modal-dialog' role='document'><div class='modal-content'>"+
			"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>"+
			"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>"+
			"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>建设单位id不可为空</label>"+
			"</div></div>"+
			"<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>"+
			"<span class='glyphicon' aria-hidden='true'></span>关闭</button>"+
			"<button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>"+
			"<span class='glyphicon' aria-hidden='true'></span>确定</button></div></div></div></div>");
			 $('#mytreeModal').modal();
			return;
		}
	
	}
		// datatables 
	if(!yifangTable){
		//tmpTable.destroy();
		yifangTable = $("#yifangTable").DataTable({
			"ordering": false,// 排序
			"serverSide": true,// 开启服务器模式
	        "scrollX": true,// 横向滚动
	        "scrollCollapse": true,
			ajax: {
	            "type": "POST",
	            "url":prc+'/sysOrgController/selectEforgBydisId.action',//请求路径
	            "contentType": 'application/x-www-form-urlencoded; charset=UTF-8',
	            "dataType":'json',
	            "data":function(d){// 查询参数
	            	d.disId =  disId;
	            	d.orgName = $("#searchEfOrgName").val();
	            	return d;
	            }
			},            
			columns: [// 对应列
				{"data": null,"title":"操作",width:"*",className:"text-center",render: function (a, b, c, d) {
	            	var btnArray = new Array();
		    		btnArray.push({"name": "选择", "fn": "selectorgvalue(\'"+c.orgId+"\',\'"+c.orgName+"\',\'"+c.orgCode+"\','"+yourFun+"')", "type": "user-button"});
		    		/*if(hadPermission.update){
		    			btnArray.push({"name": "修改", "fn": "showEditPage(\'" + c.staffId + "\')", "type": "user-button"});
		    		}*/
		    	
		    		context = {
		    				func:btnArray
		    		}
		    		var html = template(context);
		    		return html;
		    	}},
	            {"data": "orgName","title":"组织名称",className: "text-center"/*,render:function(a,b,c,d){
	            	return "<a href=\"javascript:doDetail('"+c.orgId+"')\">"+a+"</a>";
	            }*/},
	            {"data": "orgId","title":"orgId","visible": false },
				{"data": "orgCode","title":"orgCode","visible": false }, 
	        ],
	        "columnDefs": [
				{// 所有列默认值
					"targets": "_all",
					"defaultContent": '',
					render: $.fn.dataTable.render.ellipsis(22, true)
				},
				{// 最后一列添加按钮
		            targets: -1,
		            render: function (a, b, c, d) {
		                var context =
		                {
		                    func: [
		                        {"name": "详细信息", "fn": "showEditPage(\'" + c.id + "\')", "type": "primary"}
		                    ]
		                };
		                var html = template(context);
		                return html;
		            }
		        }]
			,"dom": 'rt<"pull-left mt5"i><"pull-right mt5"p><"clear">' //生成样式
		});	
	}else{
		yifangTable.ajax.reload();
	}
}
//回调函数
function selectorgvalue(orgId,orgName,orgCode,funName){
	window.parent[funName](orgId,orgName,orgCode);
}

//******************************************************************************************
//乙方人员列表
//jQuery Datatable 对象
var yifangStaffTable;
//甲方人员列表
//jQuery Datatable 对象
var searchStaffTable;
	
//查看甲方和乙方用户
//staffKind:'1'甲方人员'2'乙方人员
//funName:所需参数的回调函数,由调用人提供
//orgCode:组织编码,staffKind=1时需要提供此参数,此时disId需要传'999'
//disId:建设单位Id,staffKind=2时需要提供此参数,此时orgCode需要传'999'
//roleId:角色id,通过角色过滤数据,此参数可传可不传

function jandyStaffSearch(staffKind,yourFun,disId,orgCode,roleId){
	if(null==roleId||undefined == roleId){
		roleId=0;
	}
	var frameSrc =prc+"/sysOrgController/toPandJstaffList"; 
    $("#PandJstaffiframe").attr("src", frameSrc+"?staffKind="+staffKind+"&yourFun="+yourFun+"&disId="+disId+"&orgCode="+orgCode+"&roleId="+roleId);  
    $('#PandJstaffModal').modal({ show: true, backdrop: 'static' });
}



function searchstaff(){
	var disId = $("#disId").val();
	var orgCode = $("#orgCode").val();
	var roleId = $("#roleId").val();
	var staffKind = $("#staffKind").val();
	var yourFun = $("#yourFun").val();
	if(null==roleId||undefined == roleId){
		roleId=0;
	}
	if(staffKind=='1'){
		if(disId=='999'){
			if(""==orgCode){
				if($('#mytreeModal').size==0){
					 $('#mytreeModal').empty();
					 $('body').append("<div class='modal fade' id='mytreeModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>"+
					"<div class='modal-dialog' role='document'><div class='modal-content'>"+
					"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>"+
					"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>"+
					"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>组织编码不可为空</label>"+
					"</div></div>"+
					"<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>"+
					"<span class='glyphicon' aria-hidden='true'></span>关闭</button>"+
					"<button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>"+
					"<span class='glyphicon' aria-hidden='true'></span>确定</button></div></div></div></div>");
					 $('#mytreeModal').modal();
					return;
				}
			}
		}
		searchJstaff(staffKind,orgCode,roleId,yourFun);
	}else if(staffKind='2'){
		if(orgCode=='999'){
			if(""==disId){
				 $('#mytreeModal').empty();
				 $('body').append("<div class='modal fade' id='mytreeModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>"+
				"<div class='modal-dialog' role='document'><div class='modal-content'>"+
				"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>"+
				"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>"+
				"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>建设单位id不可为空</label>"+
				"</div></div>"+
				"<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>"+
				"<span class='glyphicon' aria-hidden='true'></span>关闭</button>"+
				"<button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>"+
				"<span class='glyphicon' aria-hidden='true'></span>确定</button></div></div></div></div>");
				 $('#mytreeModal').modal();
				return;
			}
		}
		searchEfStaff(staffKind,disId,roleId,yourFun);
	}
	var frameSrc =prc+"/sysOrgController/toPandJstaffList";
	$("#PandJstaffiframe").attr("src", frameSrc);  
	$('#PandJstaffModal').modal({ show: true, backdrop: 'static' });
}

//******************************************************************************************

function searchJstaff(staffKind,orgCode,roleId,yourFun){
//		tmpTable.destroy();
//		$("#emptyStaffTable").empty();
		$("#emptyStaffDiv").hide();
		$("#searchStaffDiv").show();
		if(!searchStaffTable){			
			// datatables 
			searchStaffTable = $("#searchStaffTable").DataTable({
				"ordering": false,// 排序
				"serverSide": true,// 开启服务器模式
		        "scrollX": true,// 横向滚动
		        "scrollCollapse": true,

				ajax: {
		            "type": "POST",
		            "url":prc+'/sysOrgController/selectStaffList.action',//请求路径
		            "contentType": 'application/x-www-form-urlencoded; charset=UTF-8',
		            "dataType":'json',
		            "data":function(d){// 查询参数
		            	d.oCode = orgCode;
		            	d.staffKind = staffKind;
		              	d.orgName = $("#searchjfOName").val();
		            	d.staffName = $("#searchjfstaffName").val();
		            	if(null!=roleId){
		            		d.roleId = roleId;	
		            	}else{
		            		d.roleId = 0;
		            	}
		                return d;
		                }
				},            
				columns: [// 对应列
					{"data": null,"title":"操作",width:"*",className:"text-center",render: function (a, b, c, d) {
		            	var btnArray = new Array();
			    		btnArray.push({"name": "选择", "fn": "selectStaffValue(\'"+c.orgId+"\',\'"+c.orgName+"\',\'"+c.staffId+"\',\'"+c.staffName+"\',\'"+c.staffOrgId+"\','"+yourFun+"')", "type": "user-button"});
			    	
			    		context = {
			    				func:btnArray
			    		}
			    		var html = template(context);
			    		return html;
			    	}},
			            {"data": "staffName","title":"人员姓名",className: "text-center"},
			            {"data": "loginName","title":"人员账号",width:"120",className: "text-center"},
			            {"data": "orgType","title":"岗位类别",className: "text-center",render: function (a, b, c, d){
							if('F' == a)return '主岗';else return '兼职';
						}},
			            {"data": "orgName","title":"所属组织",className: "text-center"},
			            {"data": "OrgId","title":"id","visible": false },
						{"data": "staffOrgId","title":"id","visible": false },
						{"data": "staffId","title":"id","visible": false },

					 
		        ],
		        "columnDefs": [
					{// 所有列默认值
						"targets": "_all",
						"defaultContent": '',
						render: $.fn.dataTable.render.ellipsis(22, true)
					}]
				,"dom": 'rt<"bottom"ip><"clear">' //生成样式
			});	
		}else{
		searchStaffTable.ajax.reload();
		}
		
	}
//乙方人员的列表
function searchEfStaff(staffKind,disId,roleId,yourFun){
//	tmpTable.destroy();
//	$("#emptyStaffTable").empty();
	$("#emptyStaffDiv").hide();
	$("#yifangStaffDiv").show();
	if(!yifangStaffTable){		
		// datatables 	
		yifangStaffTable = $("#yifangStaffTable").DataTable({
			"ordering": false,// 排序
			"serverSide": true,// 开启服务器模式
	        "scrollX": true,// 横向滚动
	        "scrollCollapse": true,
			ajax: {
	            "type": "POST",
	            "url":prc+'/sysOrgController/selectStaffList.action',//请求路径
	            "contentType": 'application/x-www-form-urlencoded; charset=UTF-8',
	            "dataType":'json',
	            "data":function(d){// 查询参数
	            	d.disId = disId;
	            	d.staffKind=staffKind;
	            	d.orgName = $("#searchEfOName").val();
	            	d.staffName = $("#searchEfstaffName").val();
	            	if(null!=roleId){
	            		d.roleId = roleId;	
	            	}else{
	            		d.roleId = 0;
	            	}
	                return d;
	                }
	            
			},            
			columns: [// 对应列
				{"data": null,"title":"操作",width:"*",className:"text-center",render: function (a, b, c, d) {
	            	var btnArray = new Array();
		    		btnArray.push({"name": "选择", "fn": "selectStaffValue(\'"+c.orgId+"\',\'"+c.orgName+"\',\'"+c.staffId+"\',\'"+c.staffName+"\',\'"+c.staffOrgId+"\',"+yourFun+")", "type": "user-button"});
		    	
		    		context = {
		    				func:btnArray
		    		}
		    		var html = template(context);
		    		return html;
		    	}},
	            {"data": "staffName","title":"人员姓名",className: "text-center"},
	            {"data": "loginName","title":"人员账号",width:"120",className: "text-center"},
	            {"data": "orgType","title":"岗位类别",className: "text-center",render: function (a, b, c, d){
					if('F' == a)return '主岗';else return '兼职';
				}},
	            {"data": "orgName","title":"所属组织",className: "text-center"},
	            {"data": "OrgId","title":"id","visible": false },
				{"data": "staffOrgId","title":"id","visible": false },
				{"data": "staffId","title":"id","visible": false },
				 
	        ],
	        "columnDefs": [
				{// 所有列默认值
					"targets": "_all",
					"defaultContent": '',
					render: $.fn.dataTable.render.ellipsis(22, true)
				}]
			,"dom": 'rt<"bottom"ip><"clear">' //生成样式
		});	
	}else{
		yifangStaffTable.ajax.reload();
	}
}
//人员列表的回调函数
function selectStaffValue(orgId,orgName,staffId,staffName,staffOrgId,funName){
	window.parent[funName](orgId,orgName,staffId,staffName,staffOrgId);
}
